
package com.netease.xone.fragment;

import java.util.ArrayList;

import protocol.XoneCallBack;
import protocol.XoneService;
import Utils.StackTraceUtil;
import Utils.XoneUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.netease.log.NTLog;
import com.netease.pkgRelated.XoneConsts;
import com.netease.pkgRelated.XoneConsts.MENU_ID;
import com.netease.xone.R;
import com.netease.xone.widget.CustomDialog.OnCustomDlgCancelListener;

import db.manager.ManagerAccount;
import db.manager.ManagerAccount.Account;

public class FragmentMenuFeedBack extends FragmentBase {

    public static final String TAG = FragmentMenuFeedBack.class.getSimpleName();
    public static final int TYPE_BASE = 11;

    private int mTrasactionId;
    private int mCheckIndex = 0;
    
    private String mInfoId;
    private String mTitle;
    private EditText mContent;
    
    private ArrayList<CheckBox> mList = new ArrayList<CheckBox>();
    
    public static FragmentMenuFeedBack newInstance(String infoId, String title) {
        FragmentMenuFeedBack f = new FragmentMenuFeedBack();
        Bundle b = new Bundle();
        b.putString(XoneConsts.BUNDLE_KEY.INFO_ID, infoId);
        b.putString(XoneConsts.EXTRA_PATH, title);
        f.setArguments(b);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        getActivity().setTitle(R.string.infodetail_menu_feedback);
        mInfoId = getArguments().getString(XoneConsts.BUNDLE_KEY.INFO_ID);
        mTitle = getArguments().getString(XoneConsts.EXTRA_PATH);
        View view = inflater.inflate(R.layout.fragment_menu_feedback, container, false);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        CheckBox checkBox = null;
        mList.clear();
        
        String id = "";
        int resIdentifier = 0;
        for (int i = 0; i < 4; i++) {
            id = "check" + i;
            resIdentifier = getResources().getIdentifier(id, "id", getActivity().getPackageName());
            
            checkBox = (CheckBox)view.findViewById(resIdentifier);
            checkBox.setOnCheckedChangeListener(mOnCheckedChangeListener);
            checkBox.setTag(i);
            mList.add(checkBox);
        }
        
        mContent = (EditText)view.findViewById(R.id.content);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        com.actionbarsherlock.view.MenuItem item = menu.add(Menu.NONE, MENU_ID.menu_feedback_send,
                0, getString(R.string.done));
        item.setIcon(R.drawable.icon_action_done_selector);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        switch (item.getItemId()) {
            case MENU_ID.menu_feedback_send:
                send();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private String getType(int index) {
        int resId = 0;
        switch (index) {
            case 0:
                resId = R.string.detail_feedback_1;
                break;
            case 1:
                resId = R.string.detail_feedback_2;
                break;
            case 2:
                resId = R.string.detail_feedback_3;
                break;
            case 3:
                resId = R.string.detail_feedback_4;
                break;
        }
        String value = getString(resId);
        
        return value;
    }
    
    private String getFeedBackTitle(int index) {
        return XoneConsts.APP_OS_NAME + XoneUtil.getNumberVersion(getActivity()) + " " + getType(mCheckIndex);
    }

    private String getContent() {
        StringBuffer sb = new StringBuffer();
        
        Account acc = ManagerAccount.getInstance().getCurrentAccount();
        sb.append("UserName=").append(acc.mNickName);
        sb.append(";UserId=").append(acc.mUserId).append("\n");
        sb.append("资讯ID:").append(mInfoId).append("\n");
        sb.append("资讯源:").append(mTitle).append("\n");
        sb.append("反馈类型:").append(getType(mCheckIndex)).append("\n");
        sb.append("反馈意见:\n").append(mContent.getText().toString()).append("\n");
        
        return sb.toString();
    }
    
    private void send() {
        String value = null;
        
        // 获取内容 发送反馈, 如果内容为空提示
        value = mContent.getText().toString();
        if (TextUtils.isEmpty(value.trim())) {
            showToast(R.string.detail_feedback_null);
        } else {
            Account acc = ManagerAccount.getInstance().getCurrentAccount();
           
            XoneService.getInstance().addListener(mXoneCallBack);
            mTrasactionId = XoneService.getInstance().doFeedBack(XoneConsts.FEEDBACK_TYPE.Info, acc.mNickName, getFeedBackTitle(mCheckIndex), getContent(), null, acc.mUserId, getActivity());
            showCustomDialog(getActivity(), getString(R.string.detail_feedback_doing), mOnCustomDlgCancelListener);
        }
        
    }
    
    private OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int index = (Integer)buttonView.getTag();
            if (!isChecked) {
                if (index == mCheckIndex) {
                    buttonView.setChecked(true);
                }
                return;
            }
            
            mCheckIndex = index;
            CheckBox checkBox = null;
            for (int i = 0; i < mList.size(); i++) {
                checkBox = mList.get(i);
                if (i != index) {
                    checkBox.setChecked(false);
                } 
            }
        }
        
    };
    
    private OnCustomDlgCancelListener mOnCustomDlgCancelListener = new OnCustomDlgCancelListener() {

        @Override
        public void onCancel() {
            XoneService.getInstance().cancelTransaction(mTrasactionId);
        }
    };

    @Override
    public void onDestroy() {
        XoneService.getInstance().removeListener(mXoneCallBack);
        mXoneCallBack = null;
        super.onDestroy();
        
        if (null != mList) {
            mList.clear();
        }
        mList = null;
        mContent = null;
    }

    XoneCallBack mXoneCallBack = new XoneCallBack() {
        @Override
        public void onFeedBack1(int transactionId, Object obj) {
            if (mTrasactionId == transactionId) {
                mTrasactionId = -1;
                closeCustomDialog();
                showToast(R.string.detail_feedback_ok);
                getActivity().finish();
            }
        };
        
        @Override
        public void onFeedBack1Error(int transactionId, int errCode, String err) {
            if (mTrasactionId == transactionId) {
                mTrasactionId = -1;
                closeCustomDialog();
                showToast(R.string.detail_feedback_fail);
            }
        };
        
    };
}
